<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('User_model','users');
    $this->load->model('Operation_model', 'oper');
	}

	public function index()
	{
		$this->load->view('login');
	}

	public function login(){
		//IP封锁检查
      if(!$this->users->isIPBlock()){
          jechof('连续5次登录失败,请五分钟后再试');
          exit;
      }
      $username = $this->input->post('username');
      //密码取16位MD5
      $password = substr(md5($this->input->post('password')), 8, 16);

      $userInfo = $this->users->getUserInfo(array(
          'username' => $username,
          'password' => $password,
          'status' => 0
      ));

      if(!$userInfo){
          //增加ipblock
          $this->users->addIpBlock();
          //登录失败
          jechof('登录失败!');
      }else{
          $_SESSION = $userInfo;
          $_SESSION['LOGINTIME'] = time();
          $this->oper->add(array(
            'operation'=>'登录成功',
            'ip'=> $this->input->ip_address(),
            'uid'=>$_SESSION['id'],
            'time'=> date('Y-m-d h:i:s',time())
          ));
          jechos('登录成功!');
      }
	}

  public function logout(){
    //$_SESSION = array('');
    $this->oper->add(array(
            'operation'=>'退出登录成功',
            'ip'=> $this->input->ip_address(),
            'uid'=>$_SESSION['id'],
            'time'=> date('Y-m-d h:i:s',time())
          ));
    foreach ($_SESSION as $key => $value) {
      unset($_SESSION[$key]);
    }
    if(empty($_SESSION['id'])){
      header("Location:/index.php");
    }
    
  }

}

/* End of file login.php */
/* Location: ./application/controllers/login.php */

